<?php
namespace app\wenchuang\controller;

use app\wenchuang\model\ArticleNoticeModel;
use app\wenchuang\validate\ArticleNoticeValidate;
use cmf\controller\AdminBaseController;

class ArticleNoticeController extends AdminBaseController
{
    // 列表
    public function index()
    {
        $title = $this->request->param('title');
        $startTime = $this->request->param('start/s');
        $endTime = $this->request->param('end/s');
        $status = $this->request->param('status');
        $where = [];
        if ($title) {
            $where[] = ['title', 'like', '%'.$title.'%'];
        }
        // 只搜开始时间
        if (!empty($startTime) && empty($endTime)) {
            $where[] = ['create_time', '>=', strtotime($startTime)];
        }
        // 只搜结束时间
        if (empty($startTime) && !empty($endTime)) {
            $where[] = ['create_time', '<=', strtotime($endTime)];
        }
        // 开始时间和结束时间
        if (!empty($startTime) && !empty($endTime)) {
            $where[] = ['create_time', 'between time', [strtotime($startTime), strtotime($endTime)]];
        }
        if ($status != '') {
            $where[] = ['status', '=', $status];
        }
        $article = ArticleNoticeModel::order(['list_order' => 'desc', 'create_time' => 'desc'])
            ->where($where)
            ->paginate(10);
        $article->appends($this->request->param());
        $pageButton = $article->render();
        $this->assign('pageButton', $pageButton);
        $this->assign('article', $article);
        return $this->fetch();
    }

    // 显示添加页面
    public function add()
    {
        return $this->fetch();
    }

    // 添加保存
    public function addPost()
    {
        $data = $this->request->param();
        $validate = new ArticleNoticeValidate();
        $result = $validate->scene('add')->check($data);
        if ($result !== true) {
            $this->error($validate->getError());
        }
        $content = htmlspecialchars_decode($data['content']);
        $content = cmf_replace_content_file_url($content, true);
        $content = htmlspecialchars($content);
        $data['content'] = $content;
        $model = new ArticleNoticeModel();
        if ($model->save($data)) {
            $this->success('添加成功', 'Article_notice/index');
        }
        $this->error('添加失败');
    }

    // 显示编辑也能
    public function edit()
    {
        $id = $this->request->param('id/d');
        $article = ArticleNoticeModel::find($id);
        $article['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($article['content']));
        $this->assign('article', $article);
        return $this->fetch();
    }

    // 编辑保存
    public function editPost()
    {
        $data = $this->request->param();
        $validate = new ArticleNoticeValidate();
        $result = $validate->scene('edit')->check($data);
        if ($result !== true) {
            $this->error($validate->getError());
        }
        // 处理富文本
        $data['content'] = htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($data['content']), true));
        $article = ArticleNoticeModel::find($data['id']);
        $article->title = $data['title'];
        $article->abstract = $data['abstract'];
        $article->image = $data['image'];
        $article->content = $data['content'];
        $article->author = $data['author'];
        if ($article->save()) {
            $this->success('保存成功', 'Article_notice/index');
        }
        $this->error('保存失败');
    }

    // 软删除
    public function softDelete()
    {
        $id = $this->request->param('id/d');
        $article = ArticleNoticeModel::find($id);
        if ($article->delete()) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }

    // 修改文章状态
    public function changeArticleStatus($id)
    {
        $article = ArticleNoticeModel::find($id);
        if ($article->status == 0) {
            $article->status = 1;
            $article->save();
        } else {
            $article->status = 0;
            $article->save();
        }
        $this->success('修改成功');
    }
}
